package com.aaa.controller;

import com.aaa.entity.Menu;
import com.aaa.entity.MyReturn;
import com.aaa.service.MenuService;
import com.aaa.util.TreeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单权限表(Menu)表控制层

 */
@RestController
@RequestMapping("menu")
public class MenuController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    private MyReturn myReturn= new MyReturn();

    /**
     * 页面跳转
     *


     */
    @GetMapping("toShowMenu.do")
    public ModelAndView toMenu() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("menuNew");
        return modelAndView;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne.do")
    public Menu selectOne(Integer id) {
        return this.menuService.queryById(id);
    }

    /**
     * 查询全部菜单，用于菜单页的展示


     */

    @GetMapping("queryAllMenu.do")
    public MyReturn queryAllMenu() {
        List<Menu> menus = menuService.queryAllMenu();
        myReturn.setData(menus);
        return super.getSuccessResult(myReturn);
    }

    /**
     * 查询所有菜单，用于添加和编辑菜单时的生成树
     *
     * @return List
     */
    @GetMapping("queryAllMenus.do")
    public List queryAllMenus() {
        List<Menu> menus = menuService.queryAllMenu();
        return TreeUtil.fromMenuListToTreeList(menus);
    }

    /**
     * 假删除
     *
     * @param menuId 主键


     */

    @RequestMapping("deleteMenuById.do")
    public MyReturn deleteMenuById(Integer menuId) {
        int i = menuService.deleteMenuById(menuId);
        if (i > 0) {
            return super.getSuccessResult(myReturn);
        } else {
            return super.getFailResult(myReturn);
        }
    }
    @RequestMapping("deleteMenu.do")
    public MyReturn deleteById(Integer menuId) {
        boolean deleteById = menuService.deleteById(menuId);
        if (deleteById){
            return super.getSuccessResult(myReturn);
        } else {
            return super.getFailResult(myReturn);
        }
    }

    /**
     * 添加菜单
     *
     * @param menu 实体类
     * @return ReturnBean
     */

    @PostMapping("insertMenu.do")
    public MyReturn insertMenu(Menu menu) {
        if (menu.getParentId() == null) {
            menu.setParentId(0);
        }
        Menu insert = menuService.insert(menu);
        if (insert != null) {
            return super.getSuccessResult(null);
        }
        return super.getFailResult(myReturn);
    }

    /**
     * 更新菜单
     *
     * @param menu 实体类
     * @return ReturnBean
     */

    @RequestMapping("updateMenu.do")
    public MyReturn updateMenu(Menu menu) {
        try {
            Menu update = menuService.update(menu);
            myReturn.setData(update);
            return super.getSuccessResult(myReturn);
        } catch (Exception e) {
            return super.getFailResult(myReturn);
        }
    }

    /**
     * 根据id查找父节点
     *
     * @param menuId 主键
     * @return ReturnBean
     */

    @GetMapping("findParentNameId.do")
    public MyReturn findParentNameId(Integer menuId) {
        Menu parent = menuService.findParentNameId(menuId);
        myReturn.setData(parent);
        return super.getSuccessResult(myReturn);
    }
}
